﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.Text.RegularExpressions;
using Microsoft.VisualBasic;

namespace ProxyServerEmulator
{
    public partial class frmBannedList : Form
    {
        public frmBannedList()
        {
            InitializeComponent();
            
            // Add Columns to ListView lstBanned
            lstBanned.Columns.Add("Site Name");
            lstBanned.Columns[0].Width = lstBanned.Width / 2;
            lstBanned.Columns.Add("URL");
            lstBanned.Columns[1].Width = lstBanned.Width / 2;


            // Get the list of banned sites from XML
            XmlTextReader reader = new XmlTextReader("Banned.xml");
            XmlDataDocument doc = new XmlDataDocument();
            doc.Load(reader);

            XmlNodeList sites = doc.SelectNodes("BANNEDSITES");
            for (int i = 0; i < sites[0].ChildNodes.Count; i++)
            {
                ListViewItem item = new ListViewItem(sites[0].ChildNodes[i].ChildNodes[0].InnerText, 1);
                item.SubItems.Add(sites[0].ChildNodes[i].ChildNodes[1].InnerText);
                lstBanned.Items.Add(item);
            }
            
            reader.Close();

        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            string SiteName = Interaction.InputBox("Website Name: ", "Input Site Name").ToString();
            string SiteURL = Interaction.InputBox("Website URL: ", "Input Site URL").ToString();
            if (!String.IsNullOrEmpty(SiteName) && !String.IsNullOrEmpty(SiteURL))
            {
                ListViewItem newItem = new ListViewItem(SiteName,1 );
                newItem.SubItems.Add(SiteURL);
                lstBanned.Items.Add(newItem);
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (lstBanned.SelectedItems.Count < 1)
                MessageBox.Show("Please Select Site To Delete!", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
            else
            {
                while (lstBanned.SelectedItems.Count > 0)
                {
                    lstBanned.Items.Remove(lstBanned.SelectedItems[0]);
                }
            }

        }

        private void frmBannedList_FormClosing(object sender, FormClosingEventArgs e)
        {
            // Save the listview to XML File
            XmlTextWriter savefile = new XmlTextWriter(Application.StartupPath + "\\Banned.xml", Encoding.UTF8);
            savefile.WriteStartDocument(true);
            savefile.WriteStartElement("BANNEDSITES");
            for (int i = 0; i < lstBanned.Items.Count; i++)
            {
                savefile.WriteStartElement("SITE");
                savefile.WriteElementString("SITE_NAME", lstBanned.Items[i].SubItems[0].Text);
                savefile.WriteElementString("SITE_URL", lstBanned.Items[i].SubItems[1].Text);
                savefile.WriteEndElement();
            }
            savefile.WriteEndElement();
            savefile.Close();
        }
    }
}
